<?php
class adm_news extends core_modbase {
	
	function __construct($adm)
	{
		$this->template = new template();
		$this->translator = new translator();
		$this->translator->load('common');
		$this->translator->load('admin');
		$session = new session();
		$this->acl = &$adm->acl;
				
		$menu = array(
			'mgnews' => array(
				'mnews_new' => '{AP}news/new',
				'mnews_wait' => '{AP}news/waiting',
				'mnews_list' => '{AP}news',
			)
		);
		core_signal::SmpSignal('admin::menu', $menu);
	}
	
	function PAGE_index() {
		$sql = new SQL('news');
		$this->template->assign('edit', core_settings::Get('sitepath') . 'admin/news/edit');
		$this->template->assign('new', core_settings::Get('sitepath') . 'admin/news/new');
		$this->template->assign('wait', core_settings::Get('sitepath') . 'admin/news/waiting');

		$news = $sql->select();
		foreach ($news as $elmt)
		{
			$user = new user(intval($elmt->news_author));
			$this->template->block('news', array(
				'news_time' => ldate($elmt->news_time),
				'news_author' => $user->pseudo,
				'news_state' => $this->translator->get('state_' . $elmt->news_state),
				'news_title' => $elmt->news_title,
				'news_id' => $elmt->news_id,
			));
		}
		return $this->fetch('adm-list.tpl');
	}
	
	function PAGE_new(){
		if ($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			if ($_POST['title'] == '' || $_POST['data'] == '')
			{
				$this->assign('message', 'checkerror');
			}
			else
			{
				if (!in_array($_POST['status'], array('programed', 'active', 'foread', 'unactive')))
					$_POST['status'] = 'foread';
					
				if ($_POST['sdate'] == '')
					$_POST['sdate'] = date('Y-m-d H:i:s');

				if ($_POST['edate'] == '')
					$_POST['edate'] = null;

				$sql = new SQL ('news');
				$user = new user();
				$sql->insert(array(
					'news_title' => $_POST['title'],
					'news_time' => $_POST['sdate'],
					'news_state' => $_POST['status'],
					'news_endtime' => $_POST['edate'],
					'news_author' => $user->user_id,
					'news_content' => $_POST['data'],
				));
				return $this->PAGE_index();
			}
		}
		return $this->fetch('adm-new.tpl');
	}
	
	function PAGE_edit($newsid)
	{
		$database = new SQL('news');
		$news = $database
			->where(array('news_id' => $newsid))
			->select()
			->current();

		if ($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			$news->news_title = $_POST['title'];
			$news->news_content = $_POST['content'];

			if ($_POST['edate'] == '')
				$news->news_endtime = NULL;
			elseif (preg_match('`^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$`', $_POST['edate']))
				$news->news_endtime = $_POST['edate'] . ':00';
				
			if ($_POST['sdate'] == '')
				$news->news_time = date('Y-m-d H:i:s');
			elseif (preg_match('`^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$`', $_POST['sdate']))
				$news->news_time = $_POST['sdate'] . ':00';

			if (in_array($_POST['state'], array('active','foread','unactive','programed')))
				$news->news_state = $_POST['state'];
			
			try {
				$author = new user($_POST['author']);
				$news->news_author = $author->GetId();
			} catch (ArgError $e)
			{ }
		}

		$author = new user(intval($news->news_author));

		$this->template->assign('news', array(
			'id' => $news->news_id,
			'title' => $news->news_title,
			'time' => substr($news->news_time, 0, 16),
			'end' => substr($news->news_endtime, 0, 16),
			'state' => $news->news_state,
			'content' => $news->news_content,
			'author' => $author->pseudo,
			));

		return $this->fetch('adm-edit.tpl');
	}
}
